package com.anti.modular.sys.file.controller;
import com.anti.commom.annotation.BusinessLog;
import com.anti.commom.annotation.Permission;
import com.anti.commom.api.Result;
import com.anti.commom.enums.LogAnnotionOpTypeEnum;
import com.anti.jdbc.support.page.PageParam;
import com.anti.modular.sys.file.param.SysFileInfoParam;
import com.anti.modular.sys.file.service.SysFileInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 文件信息表 控制器
 */
@RestController
@RequestMapping("sysFileInfo")
public class SysFileInfoController {

    @Resource
    private SysFileInfoService sysFileInfoService;

    /**
     * 上传文件
     */
    @PostMapping("upload")
    @BusinessLog(title = "文件信息表_上传文件", opType = LogAnnotionOpTypeEnum.OTHER)
    public Result upload(@RequestPart("file") MultipartFile file) {
        Long fileId = sysFileInfoService.saveUploadFile(file);
        return Result.data(fileId);
    }

    /**
     * 下载文件
     */
    @GetMapping("download")
    @BusinessLog(title = "文件信息表_下载文件", opType = LogAnnotionOpTypeEnum.OTHER)
    public void download(@Validated(SysFileInfoParam.detail.class) SysFileInfoParam sysFileInfoParam, HttpServletResponse response) {
        sysFileInfoService.download(sysFileInfoParam, response);
    }

    /**
     * 文件预览
     */
    @GetMapping("preview")
    public void preview(@Validated(SysFileInfoParam.detail.class) SysFileInfoParam sysFileInfoParam, HttpServletResponse response) {
        sysFileInfoService.preview(sysFileInfoParam, response);
    }

    /**
     * 分页查询文件信息表
     */
    @GetMapping("page")
//    @BusinessLog(title = "文件信息表_分页查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result page(PageParam pageParam,SysFileInfoParam sysFileInfoParam) {
        return Result.data(sysFileInfoService.page(pageParam,sysFileInfoParam));
    }

    /**
     * 获取全部文件信息表
     */
    @GetMapping("list")
//    @BusinessLog(title = "文件信息表_查询所有", opType = LogAnnotionOpTypeEnum.QUERY)
    public Result list(SysFileInfoParam sysFileInfoParam) {
        return Result.data(sysFileInfoService.list(sysFileInfoParam));
    }

    /**
     * 查看详情文件信息表
     */
    @GetMapping("detail")
    @BusinessLog(title = "文件信息表_查看详情", opType = LogAnnotionOpTypeEnum.DETAIL)
    public Result detail(@Validated(SysFileInfoParam.detail.class) SysFileInfoParam sysFileInfoParam) {
        return Result.data(sysFileInfoService.detail(sysFileInfoParam));
    }

    /**
     * 删除文件信息表
     */
    @Permission
    @PostMapping("delete")
    @BusinessLog(title = "文件信息表_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public Result delete(@RequestBody @Validated(SysFileInfoParam.delete.class) SysFileInfoParam sysFileInfoParam) {
        sysFileInfoService.delete(sysFileInfoParam);
        return Result.success("刪除成功");
    }

}
